package com.funtl.framework.java.web.orm.hibernate.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.funtl.framework.java.ssh.abstracts.AbstractEntity;
import com.funtl.framework.java.util.common.CommonBean;
import com.funtl.framework.java.util.exception.ServiceException;
import com.funtl.framework.java.web.orm.hibernate.dao.ICommonDBService;
import com.funtl.framework.java.web.orm.hibernate.dao.IHibernateDao;
import com.funtl.framework.java.web.orm.hibernate.param.HibernateParam;

@Component("commonDBService")
public class CommonDBServiceImpl implements ICommonDBService {
	private IHibernateDao hibernateDao;

	@Override
	public<T extends AbstractEntity> String insert(T entity) throws ServiceException {
		hibernateDao.insert(entity);
		return null;
	}

	@Override
	public<T extends AbstractEntity> String delete(T entity) throws ServiceException {
		hibernateDao.delete(entity, "id");
		return null;
	}

	@Override
	public<T extends AbstractEntity> String update(T entity, String id) throws ServiceException {
		HibernateParam param = new HibernateParam();
		param.setPropertyTypes(new String[] {"id"});
		param.setOperators(new String[] {"="});
		param.setPropertyValues(new String[] {id});
		
		T obj = hibernateDao.get(entity, param);
		
		CommonBean.beanToBean(entity, obj);
		hibernateDao.update(entity);
		return null;
	}

	@Override
	public<T extends AbstractEntity> String insertOrUpdate(List<T> list) throws ServiceException {
		hibernateDao.insertOrUpdate(list);
		return null;
	}

	@Override
	public<T extends AbstractEntity> long getCount(T entity) throws ServiceException {
		return hibernateDao.getCount(entity);
	}

	@Override
	public<T extends AbstractEntity> T getEntity(T entity, String id) throws ServiceException {
		HibernateParam param = new HibernateParam();
		param.setPropertyTypes(new String[] {"id"});
		param.setOperators(new String[] {"="});
		param.setPropertyValues(new String[] {id});
		return hibernateDao.get(entity, param);
	}
	
	@Override
	public <T extends AbstractEntity> T getEntityByCatch(T entity, String id) throws ServiceException {
		HibernateParam param = new HibernateParam();
		param.setPropertyTypes(new String[] {"id"});
		param.setOperators(new String[] {"="});
		param.setPropertyValues(new String[] {id});
		return hibernateDao.getByCatch(entity, param);
	}
	
	@Override
	public<T extends AbstractEntity> List<T> getList(T entity) throws ServiceException {
		return hibernateDao.getList(entity);
	}
	
	@Override
	public <T extends AbstractEntity> List<T> getList(T entity, String sort, String order) throws ServiceException {
		return hibernateDao.getList(entity, sort, order);
	}

	@Override
	public<T extends AbstractEntity> List<T> paging(T entity) throws ServiceException {
		return hibernateDao.getPagingList(entity, entity.getPageIndex(), entity.getPageSize());
	}

	@Override
	public<T extends AbstractEntity> List<T> paging(T entity, String sort, String order) throws ServiceException {
		return hibernateDao.getPagingList(entity, entity.getPageIndex(), entity.getPageSize(), sort, order);
	}
	
	@Override
	public <T extends AbstractEntity> List<T> getListByCatch(T entity) throws ServiceException {
		return hibernateDao.getListByEhcatch(entity);
	}
	
	@Override
	public <T extends AbstractEntity> List<T> getListByCatch(T entity, String sort, String order) throws ServiceException {
		return hibernateDao.getListByEhcatch(entity, sort, order);
	}
	
	@Override
	public <T extends AbstractEntity> List<T> pagingByCatch(T entity) throws ServiceException {
		return hibernateDao.getPagingListByEhcatch(entity, entity.getPageIndex(), entity.getPageSize());
	}

	@Override
	public <T extends AbstractEntity> List<T> pagingByCatch(T entity, String sort, String order) throws ServiceException {
		return hibernateDao.getPagingListByEhcatch(entity, entity.getPageIndex(), entity.getPageSize(), sort, order);
	}
	
	//getter setter
	
	public IHibernateDao getHibernateDao() {
		return hibernateDao;
	}

	@Resource
	public void setHibernateDao(IHibernateDao hibernateDao) {
		this.hibernateDao = hibernateDao;
	}

}
